//
//  UserAGView.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/2.
//

import UIKit
import YYText
class UserAGView: UIView {
    let disposeBag = DisposeBag()
    var chooseDone: (() -> Void)?
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "用户协议及隐私保护"
        label.font = UIFont.pingFangSCSemibold(ofSize: 18)
        label.textColor = Theme.color.text_d1
        label.textAlignment = .center
        return label
    }()
    
    lazy var textLabel: YYLabel = {
        let label = YYLabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        // 创建富文本字符串
        let fullText = "我已阅读并同意《用户协议》《隐私政策》"
        let attributedString = NSMutableAttributedString(string: fullText)
        // 创建并设置段落样式
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        // 设置通用字体属性
        let fullRange = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttributes([
            .font: UIFont.pingFangSCRegular(ofSize: 12), // 替换成你需要的字体
            .foregroundColor: Theme.color.text_d2, // 替换成你需要的颜色
            .paragraphStyle: paragraphStyle
        ], range: fullRange)
        // 设置《用户协议》的链接和颜色
        let userAgreementRange = (fullText as NSString).range(of: "《用户协议》")
        attributedString.yy_setTextHighlight(userAgreementRange, color: UIColor(hexString: "#45FFFF"), backgroundColor: nil) { _, _, _, _ in
            if let url = URL(string: "\(apiHost)/api/text/contract") {
                UIApplication.shared.open(url)
            }
        }
        // 设置《隐私政策》的链接和颜色
        let privacyPolicyRange = (fullText as NSString).range(of: "《隐私政策》")
        attributedString.yy_setTextHighlight(privacyPolicyRange, color: UIColor(hexString: "#45FFFF"), backgroundColor: nil) { _, _, _, _ in
            if let url = URL(string: "\(apiHost)/api/text/privacy") {
                UIApplication.shared.open(url)
            }
        }
        label.attributedText = attributedString
        return label
    }()
    
    
    private lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle("同意并继续", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 16)
        button.backgroundColor = Theme.color.text_d1
        button.allRound(25)
        return button
    }()
    
    private lazy var cancelButton: UIButton = {
        let button = UIButton()
        button.setTitle("不同意", for: .normal)
        button.setTitleColor(Theme.color.text_d3, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 16)
        button.allRound(25)
        return button
    }()
    
    private lazy var alertView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        return view
    }()
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubview(alertView)
        bindEvent()
        initViews()
    }
    
    func bindEvent(){
        cancelButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
            })
        }).disposed(by: disposeBag)
        
        confirmButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                self.chooseDone?()
            })
        }).disposed(by: disposeBag)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews() {
        alertView.addSubviews([titleLabel, textLabel, cancelButton, confirmButton])
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(24)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
        }
        
        confirmButton.snp.makeConstraints { (make) in
            make.top.equalTo(textLabel.snp.bottom).offset(30)
            make.leading.trailing.equalToSuperview().inset(24)
            make.height.equalTo(50)
        }
        
        cancelButton.snp.makeConstraints { (make) in
            make.top.equalTo(confirmButton.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview().inset(24)
            make.height.equalTo(50)
        }
        
        alertView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(44)
            make.centerY.equalToSuperview()
            make.top.greaterThanOrEqualTo(titleLabel.snp.top).offset(-24)
            make.bottom.greaterThanOrEqualTo(cancelButton.snp.bottom).offset(24)
        }
        
        alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.alertView.transform = .identity
        }, completion: nil)
    }
}
